package com.github.od.context;

import java.util.HashMap;
import java.util.Map;

import com.github.od.constants.CommonConstants;
import com.github.od.util.StringHelper;

/**
 * 基础全局上下文拦截器
 * 
 * @author xieweikun
 * @version 2017年11月15日 下午9:16:48
 */
public class BaseContextHandler {

	public static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<Map<String, Object>>();

	public static void set(String key, Object value) {
		Map<String, Object> map = threadLocal.get();
		if (map == null) {
			map = new HashMap<String, Object>();
			threadLocal.set(map);
		}
		map.put(key, value);
	}

	public static Object get(String key) {
		Map<String, Object> map = threadLocal.get();
		if (map == null) {
			map = new HashMap<String, Object>();
			threadLocal.set(map);
		}
		return map.get(key);
	}

	public static String getUserId() {
		return StringHelper.getObjectValue(get(CommonConstants.CONTEXT_KEY_USER_ID));
	}
	public static void setUserId(String value) {
		set(CommonConstants.CONTEXT_KEY_USER_ID, value);
	}

	public static String getUserName() {
		return StringHelper.getObjectValue(get(CommonConstants.CONTEXT_KEY_USER_NAME));
	}
	public static void setUserName(String value) {
		set(CommonConstants.CONTEXT_KEY_USER_NAME, value);
	}
	
	public static String getName() {
		return StringHelper.getObjectValue(get(CommonConstants.CONTEXT_KEY_NAME));
	}
	public static void setName(String value) {
		set(CommonConstants.CONTEXT_KEY_NAME, value);
	}

	public static void remove() {
		threadLocal.remove();
	}

}
